<?php
/*************************************************

用户公共控制器 - the
Author: Reload <17-10-23>

*************************************************/
namespace Home\Controller;
use Think\Controller;
class FinancialController extends CommonController {
    public function financial() {
        $val = I('get.val');
        if($val==2) {
            //精选平台
            $map['is_hot'] = 1;
        } else {
            //热门推荐
           // $map['is_hot'] = 0;
        }
			$map['status'] = 0;
			$map['id'] =array('neq',2);
			$order ='`order` asc';
            $platform = D('Platform')->get_all($map,$file="*",$order);
            $map = array();
            foreach($platform as $k => $v) {
                $map['platform_id'] = $v['id'];
                $order = "deadline asc";
                $plat_sch = D('PlatformScheme')->get_one($map,$order);
                $platform[$k]['sch_start_amount'] = $plat_sch['start_amount'];
                $platform[$k]['sch_user_roi'] = $plat_sch['start_amount']*$plat_sch['user_roi']/100;
                $plat_sch_count = D('PlatformObject')->count($map);
                $platform[$k]['plat_sch_count'] = $plat_sch_count;
            }
    //   print_r($platform);
        $this->assign('platform',$platform);
        $this->display();
    }

    public function sort() {
        $sort = I('post.sort');
        $val = I('post.val');
                if($val==2) {
                    //精选平台
                    $map['is_hot'] = 1;
                } else {
                    //热门推荐
                   // $map['is_hot'] = 0;
                }
                if($sort==2) {
                    //首投返利
                    $map['user_award'] = array('gt',0);
                } else if($sort==3) {
                    //复投加息
                    $map['rebate'] = array('gt',0);
                } else if($sort==4) {
                    //多投多返roi
                    $map['user_roi'] = array('gt',0);
                }else if($sort==1) {
					$order ='`order` asc';
				}
				$map['status'] = 0;
                $platform = D('Platform')->get_all($map,$file="*",$order);
                    $map = array();
                    foreach($platform as $k => $v) {
                        $map['platform_id'] = $v['id'];
                        $order = "deadline asc";
                        $plat_sch = D('PlatformScheme')->get_one($map,$order);
                        $platform[$k]['sch_start_amount'] = $plat_sch['start_amount'];
                        $platform[$k]['sch_user_roi'] = $plat_sch['start_amount']*$plat_sch['user_roi']/100;
                        $plat_sch_count = D('PlatformObject')->count($map);
                        $platform[$k]['plat_sch_count'] = $plat_sch_count;
                    }
                $this->assign('platform',$platform);
                $platfotm = $this->fetch('fina_platform');
                $this->ajaxReturn(array('platform'=>$platfotm));
    }
	
    public function index(){
        //广告位
        if(IS_POST){
            $order = I('post.order');
            switch ($order) {
                case '1':
                     $OrderBy = ' `user_award` desc';
                    break;
                case '2':
                     $OrderBy = 'id desc';
                    break;
                case '3':
                     $map['is_hot'] = 0;
                    break;
                case '4':
                     $map['rebate'] = array('gt',0);
                    break;
            }
        }
        $wei_lc_top = R('Ad/get_ad',array('tag'=>'wei_lc_top'));
        $map['status']=0;
        $plat_list = D('Platform')->get_all($map,$field='*',$OrderBy);
        //echo D('Platform')->_sql();
        foreach ($plat_list as $k => $v) {
			  //统计平台投资方案数
            $map = array();
            $map['platform_id'] = $v['id'];
            $plat_list[$k]['scheme_count']=D('PlatformScheme')->count($map);
            $start_amount = (int)$v['start_amount'];
            $count = strlen($start_amount);
            $start_num = substr($start_amount,0,1);
            if($count==2){
                $plat_list[$k]['start_num'] = $start_num."<span>十元起</span>";
            }else if($count==3){
                $plat_list[$k]['start_num'] = $start_num."<span>千元起</span>";
            }else if($count==4){
                $plat_list[$k]['start_num'] = $start_num."<span>千元起</span>";
            }else if($count==5){
                $plat_list[$k]['start_num'] = $start_num."<span>万元起</span>";
            }
            
            if(strpos($v['start_deadline'],"个月")){
                $start_day = str_replace("个月","",$v['start_deadline']);
                $plat_list[$k]['start_day'] =$start_day."<span>个月起</span>";
            }else if(strpos($v['start_deadline'],"天")){
                $start_day = str_replace("天","",$v['start_deadline']);
                $plat_list[$k]['start_day'] =$start_day."<span>天起</span>";
            }
            $map = array();
            $map['platform_id'] = $v['id'];
            $circle = D('Circle')->get_one($map);
            if($circle){
                $plat_list[$k]['circle'] = $circle;
            }else{
                unset($plat_list[$k]);
            }
            
             
            
        }
        //print_r($plat_list);
        $this->assign('wei_lc_top',$wei_lc_top);
        $this->assign('list',$plat_list);
        if(IS_POST){
            $this->assign('list',$plat_list);
            if($order==4){
                $this->assign('is_rebate',1);
            }
            $content = $this->fetch('financial-item');
            
            $this->ajaxReturn(array('content'=>$content));
        }
        $this->display();
            
    }

  //   //活动页（红包详情页）
  //   public function event(){
  //       //判断是否是红包页面
  //       $packet = I('get.packet');
  //       $id = I('get.id');
		// //print_r($_SESSION);
  //       $map['id'] = $id;
  //       $info = D('Platform')->get_one($map);
  //       $map = array();
  //       $map['platform_id'] = $id;
  //       $info['circle'] = D('Circle')->get_one($map);
  //       $info['cricel_leve']= R('Circle/get_cricel_leve',array('platform_id'=>$id));
  //      // print_r($info);
  //       $event_years_top = 0;
  //       if($packet){
            
  //           $list = R('Packet/get_redpacket',array('user_id'=>$packet,'platform_id'=>$id));
  //          foreach ($list['scheme'] as $k => $v) {
  //               if($v['deadline_unit']==0){
  //                   $tz_award= $v['start_amount']*$v['yields']/100/12*$v['deadline'];
  //                   $tz_all_award=sprintf("%.2f", $tz_award+$v['p_user_award']);
  //                   $tz_award = sprintf("%.2f",$tz_award);
  //                   $list['scheme'][$k]['tz_award'] = $tz_award;
  //                   $list['scheme'][$k]['tz_all_award']  = $tz_all_award;
  //                   $event_years =  sprintf("%.2f",$tz_all_award/$v['start_amount']*12/$v['deadline'])*100;
  //                   if($event_years>$event_years_top){
  //                       $event_years_top = $event_years;
  //                   }
  //                   $list['scheme'][$k]['event_years'] = $event_years;
  //               }else{
  //                   $tz_award= $v['start_amount']*$v['yields']/100/360*$v['deadline'];
  //                   $tz_all_award=sprintf("%.2f", $tz_award+$v['p_user_award']);
  //                   $tz_award = sprintf("%.2f",$tz_award);
  //                   $list['scheme'][$k]['tz_award'] = $tz_award;
  //                   $list['scheme'][$k]['tz_all_award']  = $tz_all_award;
  //                   $event_years =  sprintf("%.2f",$tz_all_award/$v['start_amount']*12/$v['deadline'])*100;
  //                   $list['scheme'][$k]['event_years'] = $event_years;
  //                   if($event_years>$event_years_top){
  //                       $event_years_top = $event_years;
  //                   }
  //               }

  //               if($info['team_type']=='CPA+CPS'){
  //                   if($v['deadline_unit']==0){
  //                       $tz_award= $v['start_amount']*$v['yields']/100/12*$v['deadline'];
  //                       $ft_award= $v['start_amount']*$v['rebate']/100/12*$v['deadline'];

  //                       $tz_all_award=sprintf("%.2f", $tz_award+$ft_award);
  //                       $tz_award = sprintf("%.2f",$tz_award);
  //                       $list['scheme'][$k]['tz_award'] = $tz_award;
  //                       $list['scheme'][$k]['tz_all_ft']  = $tz_all_award;
  //                       $event_years_ft =$v['yields']+$v['rebate'];
  //                       $list['scheme'][$k]['event_years_ft'] = $event_years_ft;
  //                   }else{
  //                       $tz_award= $v['start_amount']*$v['yields']/100/360*$v['deadline'];
  //                       $ft_award= $v['start_amount']*$v['rebate']/100/12*$v['deadline'];
  //                       $tz_all_award=sprintf("%.2f", $tz_award+$ft_award);
  //                       $tz_award = sprintf("%.2f",$tz_award);
  //                       $list['scheme'][$k]['tz_award'] = $tz_award;
  //                       $list['scheme'][$k]['tz_all_award']  = $tz_all_award;
  //                      $event_years_ft =$v['yields']+$v['rebate'];
  //                       $list['scheme'][$k]['event_years_ft'] = $event_years_ft;
  //                   }
  //               }
                
  //           }
			
		// 	$map = array();
		// 	$map['id'] = $packet;
		// 	$userinfo=D('User')->get_one($map);
		// 	$map = array();
		// 	$map['id'] = $_SESSION['user']['id'];			
		// 	$user=D('User')->get_one($map); 
  //       }else{
			
  //           //默认的理财详情
            
  //           //投资方案
  //           $map = array();
  //           $map['platform_id'] = $id;
  //           $list = D('PlatformScheme')->get_all($map);
		
  //           foreach ($list as $k => $v) {
  //               if($v['deadline_unit']==0){
  //                   $tz_award= $v['start_amount']*$v['yields']/100/12*$v['deadline'];
  //                   $tz_all_award=sprintf("%.2f", $tz_award+$v['user_award']);
  //                   $tz_award = sprintf("%.2f",$tz_award);
  //                   $list[$k]['tz_award'] = $tz_award;
  //                   $list[$k]['tz_all_award']  = $tz_all_award;
  //                   $event_years=  sprintf("%.2f",$tz_all_award/$v['start_amount']*12/$v['deadline'])*100;
		// 			$list[$k]['event_years'] = $event_years;
		// 			if($event_years>$event_years_top){
						
  //                       $event_years_top = $event_years;
  //                   }
  //               }else{
  //                   $tz_award= $v['start_amount']*$v['yields']/100/360*$v['deadline'];
  //                   $tz_all_award=sprintf("%.2f", $tz_award+$v['user_award']);
  //                   $tz_award = sprintf("%.2f",$tz_award);
  //                   $list[$k]['tz_award'] = $tz_award;
  //                   $list[$k]['tz_all_award']  = $tz_all_award;
  //                   $event_years=  sprintf("%.2f",$tz_all_award/$v['start_amount']*360/$v['deadline'])*100;
		// 			$list[$k]['event_years'] = $event_years;
  //                   if($event_years>$event_years_top){
  //                       $event_years_top = $event_years;
  //                   }
  //               }

  //                 if($info['team_type']=='CPA+CPS'){
  //                   if($v['deadline_unit']==0){
  //                       $tz_award= $v['start_amount']*$v['yields']/100/12*$v['deadline'];
  //                       $ft_award= $v['start_amount']*$v['rebate']/100/12*$v['deadline'];

  //                       $tz_all_award=sprintf("%.2f", $tz_award+$ft_award);
                        
  //                       $tz_award = sprintf("%.2f",$tz_award);
  //                       $list[$k]['tz_award'] = $tz_award;
  //                       $list[$k]['tz_all_ft']  = $tz_all_award;
  //                       $event_years_ft =$v['yields']+$v['rebate'];
                        
  //                       $list[$k]['event_years_ft'] = $event_years_ft;
  //                   }else{
  //                       $tz_award= $v['start_amount']*$v['yields']/100/360*$v['deadline'];
  //                       $ft_award= $v['start_amount']*$v['rebate']/100/12*$v['deadline'];
  //                       $tz_all_award=sprintf("%.2f", $tz_award+$ft_award);
  //                       $tz_award = sprintf("%.2f",$tz_award);
  //                       $list[$k]['tz_award'] = $tz_award;
  //                       $list[$k]['tz_all_ft']  = $tz_all_award;
  //                       $event_years_ft =$v['yields']+$v['rebate'];
  //                       $list[$k]['event_years_ft'] = $event_years_ft;
  //                   }
  //               }
                
  //           }

  //       }
		
  //       //print_r($list);
  //       $this->assign('user',$user);
  //       $this->assign('userinfo',$userinfo);
  //       $this->assign('event_years_top',$event_years_top);
  //       $this->assign('info',$info);
  //       $this->assign('list',$list);
  //       $this->display();
  //   }
  //   
  //   
        public function event(){
            $user_id = $_SESSION['user']['id'];
            if($user_id){
                //查询所有方案
                $order = "`deadline_unit` desc,`deadline` asc";
                $map['user_award'] = array('gt',0);
                $list = D('PlatformScheme')->get_all($map,$field='*',$order);
                foreach ($list as $k => $v) {
                    $map = array();
                    $map['id'] = $v['platform_id'];
                    $field = 'name';
                    $platform_info= D('Platform')->get_one($map,$field);
                    $list[$k]['platform_name'] =  $platform_info['name'];
                }
                //统计名下注册人数和返利总金额
                $map = array();
                $map['referral'] = $user_id;
                $user_count = D('User')-> count($map);

                //统计返利总额
                $map = array();
                $map['user_id'] = $user_id;
                $map['fl_user_id'] = array('gt',0);
                $fl_num = D('Bill')->sum($map);

                //查询投资记录
                //未投资
                $map = array();
                $map['referral'] = $user_id;
                $user_list = M('User')->where($map)->select();
                foreach ($user_list as $k => $v) {
                  $map = array();
                  $map['user_id'] = $v['id'];
                  $bill_list = D('Bill')->get_one($map);
                  if(!$bill_list){
                    $user_list_id[$k]['phone'] = $v['user_phone'];
                    $user_list_id[$k]['is_nomal'] = 1;
                  }
                }
                //已投资
                $map = array();
                $map['user_id'] = $user_id;
                $map['fl_user_id'] = array('gt',0);
                $tz_list = D('Bill')->get_all($map);
                foreach ($tz_list as $k => $v) {
                  $fl_user_id = $v['fl_user_id'];
                  $map = array();
                  $map['id'] = $fl_user_id;
                  $user_phone =  M('User')->where($map)->field('user_phone')->find();
                  $map = array();
                  $map['id'] = $v['platform_id'];
                  $platform_name =  M('Platform')->where($map)->field('name')->find();
                  $tz_list[$k]['phone'] =$user_phone['user_phone'];
                  $tz_list[$k]['platform_name'] =$platform_name['name'];
                }
                //print_r($tz_list);
                $this->assign('user_list_id',$user_list_id);
                $this->assign('tz_list',$tz_list);
                $this->assign('list',$list);
                $this->assign('user_count',$user_count);
                $this->assign('fl_num',$fl_num);
                $this->display();
            }else{
                header("location:/Home/User/login?callback=/Home/Financial/event"); 
            }
            

        }

}